package com.example.testrxjava.activity.search;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.testrxjava.ArticleDetailsActivity;
import com.example.testrxjava.R;
import com.example.testrxjava.adapter.HotSearchInfoAdapter;
import com.example.testrxjava.base.BaseActivity;
import com.example.testrxjava.util.Action;
import com.example.testrxjava.util.CommUtil;
import com.example.testrxjava.util.ConTract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class SearchInfoActivity extends BaseActivity {
    @BindView(R.id.commonToolbarTv)
    TextView commonToolbarTv;
    @BindView(R.id.commonToolbar)
    Toolbar commonToolbar;

    @BindView(R.id.search_info_recyclerView)
    RecyclerView search_info_recyclerView;
    @BindView(R.id.search_info_Refresh)
    SmartRefreshLayout search_info_Refresh;
    @BindView(R.id.search_info_Empty)
    TextView search_info_Empty;

    private HotSearchInfoAdapter adapter;
    private int page=0;
    private String mKey;
    private List<HotSearchBean.DataBean.DatasBean> datasBeans;
    @Override
    public int getLayoutId() {
        return R.layout.activity_search_info;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void init(Bundle savedInstanceState) {
        datasBeans=new ArrayList<HotSearchBean.DataBean.DatasBean>();
        setSupportActionBar(commonToolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);//不使用原始的toolbar的title
        }
        initData();



    }

    private void initData() {
        getData();
        Action.getInstance().hotSeachInfo(page, mKey, new HotSearchSucessCallback() {
            @Override
            public void hotSeachSucess(HotSearchBean bean) {
                datasBeans.addAll(bean.getData().getDatas());
                Log.e("da==size",datasBeans.size()+"");
                if(datasBeans.size()==0){
                    search_info_Refresh.setVisibility(View.GONE);
                    search_info_Empty.setVisibility(View.VISIBLE);
                    return;
                }

                for (int i = 0; i < datasBeans.size(); i++) {
                    Log.e("sousuoxiangqing===",datasBeans.get(i).getTitle());
                }

                search_info_recyclerView.setLayoutManager(new LinearLayoutManager(SearchInfoActivity.this));
                adapter=new HotSearchInfoAdapter(R.layout.item_home_list,datasBeans);
                search_info_recyclerView.setAdapter(adapter);

                adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        ArticleDetailsActivity.startActivityForResultByActivity(SearchInfoActivity.this
                                ,datasBeans.get(position).getLink(),
                                datasBeans.get(position).getTitle(),
                                datasBeans.get(position).getId(),
                                datasBeans.get(position).isCollect(),
                                ConTract.REQUEST_ARTICLE_ACTIVITY);
                    }
                });


            }

            @Override
            public void hotSearchFail(String error) {
                CommUtil.Toast(SearchInfoActivity.this,error);
            }
        });


        search_info_Refresh.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                Action.getInstance().hotSeachInfo(page, mKey, new HotSearchSucessCallback() {
                    @Override
                    public void hotSeachSucess(HotSearchBean bean) {
                        datasBeans.addAll(bean.getData().getDatas());
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void hotSearchFail(String error) {
                        CommUtil.Toast(SearchInfoActivity.this,error);
                    }
                });

                refreshlayout.finishLoadmore();


            }
        });

        search_info_Refresh.setOnRefreshListener(new OnRefreshListener() {


            @Override
            public void onRefresh(RefreshLayout refreshlayout) {

                page=0;
                datasBeans.clear();
                Action.getInstance().hotSeachInfo(page, mKey, new HotSearchSucessCallback() {
                    @Override
                    public void hotSeachSucess(HotSearchBean bean) {
                        datasBeans.addAll(bean.getData().getDatas());
                        if(datasBeans.size()==0){
                            search_info_Refresh.setVisibility(View.GONE);
                            search_info_Empty.setVisibility(View.VISIBLE);
                            return;
                        }
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void hotSearchFail(String error) {
                        CommUtil.Toast(SearchInfoActivity.this,error);
                    }
                });
                refreshlayout.finishRefresh();


            }
        });
        commonToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
    /**
     * 得到关键字
     */
    private void getData() {
        mKey = getIntent().getStringExtra(ConTract.KEY_ARTICLE_TITLE);
        if(TextUtils.isEmpty(mKey)){
            CommUtil.Toast(this,"空~~");
            return;
        }
        commonToolbarTv.setText(mKey);
    }

    public static void startActivityBySearchActivity(Activity activity, String key) {
        Intent intent = new Intent(activity, SearchInfoActivity.class);
        intent.putExtra(ConTract.KEY_ARTICLE_TITLE, key);
        activity.startActivity(intent);
    }
    @Override
    public void initLinstener() {

    }
}
